package it.unibz.easytravel.itemlist;

import it.unibz.easytravel.R;
import it.unibz.easytravel.db.PersonalItemsDbAdapter;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;

public class TabSelectPersonalItems extends Activity {
	private LinearLayout mSelectCheckbox, mUnSelectCheckbox;
	private PersonalItemsDbAdapter mDbHelper;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.itemlist_selectpersonalitems);
		mDbHelper = new PersonalItemsDbAdapter(this);
		mDbHelper.open();

		Display();

		Button selectButton = (Button) findViewById(R.id.Button_SelectItem);
		selectButton.setOnClickListener(selectButtonListener);

		Button unselectButton = (Button) findViewById(R.id.Button_UnselectItem);
		unselectButton.setOnClickListener(unselectButtonListener);

	}
//	public void onRestart(){
//		Display();
//	}
	public void onResume (){
		super.onResume();
		mSelectCheckbox.removeAllViews();
		mUnSelectCheckbox.removeAllViews();
		Display();
	}
	void Display() {

		mSelectCheckbox = (LinearLayout) findViewById(R.id.SelectList_checkbox);
		mUnSelectCheckbox = (LinearLayout) findViewById(R.id.UnSelectList_checkbox);

		// Take all records from database
		Cursor cursor = mDbHelper.fetchAllItems();
		// Iterate through all records and put them on checkboxes
		startManagingCursor(cursor);
		int identifier = 0;
		if (cursor.moveToFirst()) {
			do {
				CheckBox newCheckBox = new CheckBox(this);
				newCheckBox
						.setText(cursor
								.getString(cursor
										.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_NAME)));
				identifier = cursor
						.getInt(cursor
								.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_TAKEN));
				if (identifier != 0) {
					mUnSelectCheckbox.addView(newCheckBox);
				} else {
					mSelectCheckbox.addView(newCheckBox);
				}

			} while (cursor.moveToNext());
		}

	}

	// change item status to untaken in database
	private OnClickListener unselectButtonListener = new OnClickListener() {
		public void onClick(View v) {
			Cursor cursor = mDbHelper.fetchAllItems();
			startManagingCursor(cursor);
			cursor.moveToFirst();
			long id, taken = 1;

			String name = "";
			CheckBox item1;
			for (int i = 0; i < mUnSelectCheckbox.getChildCount(); i++) {
				item1 = (CheckBox) mUnSelectCheckbox.getChildAt(i);
				taken = cursor
						.getInt(cursor
								.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_TAKEN));
				//skip all items in database with taken value 0, because they are from unselect menu
				while (taken == 0) {
					cursor.moveToNext();
					taken = cursor
							.getInt(cursor
									.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_TAKEN));
				}
				if (item1.isChecked()) {
					item1.setChecked(false);
					id = cursor
							.getLong(cursor
									.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_ROWID));
					name = cursor
							.getString(cursor
									.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_NAME));
					// status untaken 0
					mDbHelper.updateItem(id, name, 0);
				}
				cursor.moveToNext();
			}
			mSelectCheckbox.removeAllViews();
			mUnSelectCheckbox.removeAllViews();
			Display();

		}
	};

	// Change item status to taken in database
	private OnClickListener selectButtonListener = new OnClickListener() {
		public void onClick(View v) {
			Cursor cursor = mDbHelper.fetchAllItems();
			startManagingCursor(cursor);
			cursor.moveToFirst();
			long id, taken = 0;

			String name = "";
			CheckBox item1;
			for (int i = 0; i < mSelectCheckbox.getChildCount(); i++) {
				item1 = (CheckBox) mSelectCheckbox.getChildAt(i);
				taken = cursor
						.getInt(cursor
								.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_TAKEN));
				//skip all items in database with taken value 1, because they are from select menu
				while (taken != 0) {
					cursor.moveToNext();
					taken = cursor
							.getInt(cursor
									.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_TAKEN));
				}
				if (item1.isChecked()) {
					item1.setChecked(false);
					id = cursor
							.getLong(cursor
									.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_ROWID));
					name = cursor
							.getString(cursor
									.getColumnIndexOrThrow(PersonalItemsDbAdapter.KEY_NAME));

					// status taken 1
					mDbHelper.updateItem(id, name, 1);
				}
				cursor.moveToNext();
			}
			mSelectCheckbox.removeAllViews();
			mUnSelectCheckbox.removeAllViews();
			Display();

		}
	};

}
